package content 
{
	import net.LoaderAdapter;
	import net.LoaderQueue;
	import flash.utils.Dictionary;
	/**
	 * ...
	 * @author Ivan
	 */
	public class Content 
	{	
		//内容的存放路径
		private var _path:String;
		
		//用来保存已载入的内容
		private var _contentPool:Dictionary = new Dictionary();
		
		//下载队列
		private var _loadQueue:LoaderQueue = new LoaderQueue();
		
		
		public function Content(path:String) 
		{
			_path = path;
		}
		
		public function each(fn:Function, list:Array):void 
		{
			for (var key:String in _contentPool)
			{
				fn(key, list);
			}
		}
		
		/**
		 * 内容是否正在下载中
		 */
		public function get isLoading():Boolean
		{
			return _loadQueue.isLoading;
		}
		
		/**
		 * 获取当前正在下载的key
		 */
		public function get currentLoadingKey():*
		{
			return _loadQueue.currentAda.params[0];
		}
		
		/**
		 * 清除下载队列
		 * @param	rg
		 */
		public function clear(rg:Boolean = true):void 
		{
			_loadQueue.stop(rg);
		}
		
		/**
		 * 载入内容
		 * @param	key	要载入内容的key,载入后getContent(key)方法就可获取内容
		 * @param	addr
		 * @param	type
		 */
		public function load(key:*, type:String, addr:String = null):void 
		{
			var adapter:LoaderAdapter = new LoaderAdapter(_path + (addr || key), type, 0, onLoadComplete);
			adapter.params = [key];
			_loadQueue.add(adapter);
		}
		
		/**
		 * 检查指定key的内容是否存在
		 * @param	key
		 * @return
		 */
		public function contails(key:*):Boolean
		{
			return _contentPool[key] != null;
		}
		
		/**
		 * 获取内容
		 * @param	key	内容键
		 * @return	要获取的内容
		 */
		public function getContent(key:*):*
		{
			return _contentPool[key];
		}
		
		/**
		 * 删除指定key的内容
		 * @param	key
		 */
		public function deleteContent(key:*):void 
		{
			delete _contentPool[key];
		}
		
/*===============================================================================================================
 * 		handlers
 ================================================================================================================*/
 
		private function onLoadComplete(data:*, key:*):void 
		{
			_contentPool[key] = data;
		}
		
	}

}